package com.unohacha.qqj.fzdr.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.homebean.HomeBannerBean;
import com.unohacha.qqj.fzdr.imp.MyCallBack;
import com.unohacha.qqj.fzdr.imp.UrlConfig;
import com.unohacha.qqj.fzdr.imp.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页图片轮播适配器
 */
public class ViewPagerAdapter extends StaticPagerAdapter {
    private int[] images = {R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e};
    //首页banner
    private List<HomeBannerBean.DataBean> mHomeBannerList = new ArrayList<>();

    @Override
    public View getView(ViewGroup container, int position) {
        ImageView  imageView = new ImageView(container.getContext());
        imageView.setImageResource(images[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return imageView;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    //获取广告列表数据
    private void getAdList(){
        Map<String,Object> params = new HashMap<>();
        /**
         * typeid
         * 1-引导页 2-启动页 3-轮播图 4-面料供求 5-制造供求 6-原料供求
         * 7-辅料供应 8-人才招聘 29-求职发布 9-房屋租赁 10-设备供求
         * 11-后道供应 12-设备维修 13-库存物资 14-其他技术 30-物资行情
         * 31-物流信息 32-快递查询 33-展会信息 34-纺织学堂 35-纺织标准
         * 36-商家讨论 37-本地黄页 38-我的数据 39-加工预约
         */
        params.put("typeid",3);
        XUtil.Post(UrlConfig.AD_LIST,params,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("code"))){
                        Gson gson = new Gson();
                        HomeBannerBean bannerBean = gson.fromJson(result,HomeBannerBean.class);
                        if (null != bannerBean){
                            if (0 != mHomeBannerList.size()){
                                mHomeBannerList.clear();
                            }

                        }
                    }else {

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

}
